﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace shop.Areas.Main.Controllers
{
    public class SanPhamController : Controller
    {
        private LongHAEntities db = new LongHAEntities();

        //
        // GET: /SanPham/

        
        public ViewResult danhmuc(string type, string ID)
        {
            if (type == "DM")
            {
                var data = db.SanPhams.Where(s => s.MaDM == ID).ToList();
                ViewBag.ten = db.DanhMucs.Where(s => s.MaDM == ID).FirstOrDefault().DanhMuc1;
                if (data == null)
                {
                    return View("404.cshtml");
                }
                return View(data);
            }
            else
            {
                if (type == "TH")
                {
                    var data = db.SanPhams.Where(s => s.MaTH == ID).ToList();
                    ViewBag.ten = db.ThuongHieux.Where(s => s.MaTH == ID).FirstOrDefault().ThuongHieu1;
                    if (data == null)
                    {
                        return View("404.cshtml");
                    }
                    return View(data);
                }
                else
                {
                    if (type == "NN")
                    {
                        var data = db.SanPhams.Where(s => s.MaNN == ID).ToList();
                        ViewBag.ten = db.NgheNghieps.Where(s => s.MaNN == ID).FirstOrDefault().NgheNghiep1;
                        if (data == null)
                        {
                            return View("404.cshtml");
                        }
                        return View(data);
                    }
                    else
                    {
                        if(type=="VA")
                        {
                            return View();     
                        }
                        else
                        {
                            return View("404.cshtml");
                        }
                        
                    }
                }
            }
        }
        public ActionResult Index()
        {
            return View(db.SanPhams.ToList());
        }

        //
        // GET: /SanPham/Details/5

        public ActionResult Details(string MaSP)
        {
            SanPham sanpham = db.SanPhams.Find(MaSP);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }
        public ActionResult Chitiet(string MaSP)
        {
            var data = db.SanPhams.Where(s => s.MaSP == MaSP).FirstOrDefault();
              if(data == null )
              {
                  return HttpNotFound();
              }
            return View(data);
        }
        //
        // GET: /SanPham/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /SanPham/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(SanPham sanpham, string[] MaMau, HttpPostedFileBase anhsp)
        {
            string da = DateTime.Now.ToString("yyyymmddhhMMss");
            var path = Server.MapPath("/images/sanpham/");
            if (anhsp != null)
            {
                sanpham.HinhAnh = da + Path.GetFileName(anhsp.FileName);
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                anhsp.SaveAs(path + sanpham.HinhAnh);
            }
            else
            {
                sanpham.HinhAnh = "noimages.jpg";
            }
           // Guid s = Guid.NewGuid();
           // sanpham.MaSP = s.ToString();
            if (ModelState.IsValid)
            {
                db.SanPhams.Add(sanpham);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(sanpham);
        }

        //
        // GET: /SanPham/Edit/5

        public ActionResult Edit(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // POST: /SanPham/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(SanPham sanpham, HttpPostedFileBase AnhMoi)
        {
             string da = DateTime.Now.ToString("yyyymmddhhMMss");
            var path = Server.MapPath("/images/sanpham/");
            if(AnhMoi==null)
            {
                sanpham.HinhAnh = sanpham.HinhAnh;
            }
           
            else 
            {
                sanpham.HinhAnh = da + Path.GetFileName(AnhMoi.FileName);
                if (!Directory.Exists(path)) Directory.CreateDirectory(path);
                //.SaveAs(path + sanpham.HinhAnh);
                AnhMoi.SaveAs(path + sanpham.HinhAnh);
            }
            if (ModelState.IsValid)
            {
                db.Entry(sanpham).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(sanpham);
        }

        //
        // GET: /SanPham/Delete/5

        public ActionResult Delete(int id = 0)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            if (sanpham == null)
            {
                return HttpNotFound();
            }
            return View(sanpham);
        }

        //
        // POST: /SanPham/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            SanPham sanpham = db.SanPhams.Find(id);
            db.SanPhams.Remove(sanpham);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}